package com.pyue.eam.regi.controller;

import cn.hutool.core.collection.CollUtil;
import com.aizuda.limiter.annotation.RateLimit;
import com.aizuda.limiter.strategy.IpKeyGenerateStrategy;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pyue.eam.admin.api.constant.ResponseWord;
import com.pyue.eam.admin.api.dto.SimpleUserInfo;
import com.pyue.eam.admin.api.entity.regi.CqTaskCheck;
import com.pyue.eam.admin.api.entity.regi.CqTaskLibEntity;
import com.pyue.eam.admin.api.entity.regi.CqTaskPlanEntity;
import com.pyue.eam.admin.api.except.ApiAsserts;
import com.pyue.eam.common.core.util.R;
import com.pyue.eam.common.log.annotation.SysLog;
import com.pyue.eam.regi.service.CqTaskLibService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 任务表
 *
 * @author lucc
 * @date 2023-10-12 20:39:55
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/cqTaskLib")
@Tag(description = "cqTaskLib", name = "任务表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CqTaskLibController {
    private final CqTaskLibService cqTaskLibService;

    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param cqTaskLib 任务表
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    // @PreAuthorize("@pms.hasPermission('eam_cqTaskLib_view')" )
    public R getCqTaskLibPage(@ParameterObject Page page, @ParameterObject CqTaskLibEntity cqTaskLib) {
        return R.ok(cqTaskLibService.datePage(page, cqTaskLib));
    }

    /**
     * TOP6任务->我的任务
     */
    @Operation(summary = "TOP6任务", description = "TOP6任务")
    @GetMapping("/topTask")
    public R<List<CqTaskLibEntity>> topTask() {
        return R.okList(cqTaskLibService.topTask());
    }

    /**
     * 获取待办任务数 0138
     * 看过则去除标记
     */
    @Operation(summary = "获取待办任务数", description = "获取待办任务数")
    @GetMapping("/getTaskNum")
    public R getTaskNum() {
        return R.ok(cqTaskLibService.getTaskNum());
    }

    /**
     * 通过id查询任务表
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{taskId}")
    public R getById(@PathVariable("taskId") Long taskId) {
        // 每次点击后标识用户已读
        return R.ok(cqTaskLibService.getDataById(taskId));
    }

    /**
     * 新增任务表
     *
     * @param cqTaskLib 任务表
     * @return R
     */
    @Operation(summary = "新增任务表", description = "新增任务表")
    @SysLog("新增任务表")
    @PostMapping
    @RateLimit(count = 1, interval = "3s", strategy = {IpKeyGenerateStrategy.TYPE}, message = ResponseWord.WAIT_TIME)
    public R save(@RequestBody @Valid CqTaskLibEntity cqTaskLib) {
        return R.ok(cqTaskLibService.saveTask(cqTaskLib));
    }

    /**
     * 修改任务表
     *
     * @param cqTaskLib 任务表
     * @return R
     */
    @Operation(summary = "修改任务表", description = "修改任务表")
    @SysLog("修改任务表")
    @PutMapping
    public R updateById(@RequestBody @Valid CqTaskLibEntity cqTaskLib) {
        return R.ok(cqTaskLibService.updateDataById(cqTaskLib));
    }

    /**
     * 通过id删除任务表
     *
     * @param ids taskId列表
     * @return R
     */
    @Operation(summary = "通过id删除任务表", description = "通过id删除任务表")
    @SysLog("通过id删除任务表")
    @DeleteMapping
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(cqTaskLibService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 热门搜索关键字
     */
    @GetMapping("hotSearchTask")
    public R<List<String>> hotSearchTask() {
        return R.ok(CollUtil.newArrayList("比亚迪", "纳恩", "长城", "92-001", "门把手"));
    }

    /**
     * 团长邀请职工入任务
     */
    @Operation(summary = "团长邀请职工入任务", description = "团长邀请职工入任务")
    @SysLog("团长邀请职工入任务")
    @GetMapping("workersBindTask/{taskId}/{workIds}")
    public R<List<SimpleUserInfo>> workersBindTask(@PathVariable("taskId") Long taskId, @PathVariable("workIds") String workIds) {
        return R.okList(cqTaskLibService.workersBindTask(taskId, workIds));
    }


    /**
     * 团长申请企业验收任务
     */
    @Operation(summary = "团长申请企业验收任务", description = "团长申请企业验收任务")
    @SysLog("团长申请企业验收任务")
    @PostMapping("appleCheckCqTask")
    public R<Boolean> appleCheckCqTask(@RequestBody CqTaskCheck cqTaskCheck) {
        return R.ok(cqTaskLibService.appleCheckCqTask(cqTaskCheck));
    }

    /**
     * 企业认可方案
     */
    @Operation(summary = "企业认可方案", description = "企业认可方案")
    @PostMapping("/acceptTaskPlan")
    public R<Boolean> acceptTaskPlan(@RequestBody CqTaskPlanEntity cqTaskPlanEntity) {
        ApiAsserts.isTrue(cqTaskPlanEntity.getTaskId() != null, "任务不存在", cqTaskPlanEntity.getId() != null, "任务方案不存在");
        return R.ok(cqTaskLibService.acceptTaskPlan(cqTaskPlanEntity.getTaskId(), cqTaskPlanEntity.getId()));
    }

    /**
     * 企业验收任务
     */
    @Operation(summary = "企业验收任务", description = "企业验收任务")
    @SysLog("企业验收任务")
    @PostMapping("checkCqTask")
    public R<Boolean> checkCqTask(@RequestBody CqTaskCheck cqTaskCheck) {
        return R.ok(cqTaskLibService.checkCqTask(cqTaskCheck));
    }
}
